﻿	<jsp:include page="header.jsp">
				<jsp:param value="item6" name="item"/>
	</jsp:include> 
	<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
	<%@ taglib prefix="s" uri="/struts-tags"%>
	<section class="content">
		<section class="widget">
			<header>
				<span class="icon">&#128196;</span>
				<hgroup>
					<h1>用户管理</h1>
					<h2>员工列表</h2>
				</hgroup>
			</header>	
			<div class="content">
				<div id="baseInfo">
					<div class="btnPos">
						<input type="button" id="addItem" class="button white" value="增加"/>
						<input type="button" name="operate" id="deleteItem" class="button white" value="删除">
			    		<input type="button" id="modifyItem" name="operate" class="button white" value="修改"/>
					</div>
					
	    			<div id="useritem"></div>
	    			<div id="userpagelist" class="pagelist"></div>
	    			
	    		</div>
			</div>
	    			
			    	<div id="popup" class="hide">
				   		<div class="tabs" id="tabs">
						    <ul>
						        <li class="on">批量导入</li>
						        <li>逐个添加</li>
						    </ul>
						    
						    <div>
						    	<form action="addBatchUserInfo" method="post" enctype="multipart/form-data">
							    	<input type="file" name="file"><br/>
			      					<input type="submit" name="uploadinfo" class="button white" value="上传">
							    </form>
						    </div>
						    
						    <div class="hide">
								<table id="myTable">
									<tr>
										<td>员工号</td>
										<td><input name="userid" type="text"/></td>
										<td>员工名</td>
										<td><input name="userName" type="text"/></td>
										<td>密码</td>
										<td><input name="pwd" type="text" minlength="6"/></td>
									</tr>
									<tr>
										<td>性别</td>
										<td>
											<select name="gender" id="gender">
												<option value="男">男</option>
												<option value="女">女</option>
											</select>
										</td>
										<td>部门</td>
										<td><select name="departmentselect" id="departmentselect"></select></td>
										<td>职位</td>
										<td><input name="position" type="text"/></td>
									</tr>
									<tr>
										<td>用户组</td>
										<td><select name="usergroupselect" id="usergroupselect"></select></td>
										<td>邮箱</td>
										<td><input name="email" type="email"/></td>
										<td>权限</td>
										<td>
											<select name="authority" id="authority"/>
												<option value="管理员">管理员</option>
												<option value="员工">员工</option>
											</select>
										</td>
									</tr>
								</table>
								<div class="buttons">
									<input value="完成" type="button" onclick="addinfo()" class="button white">
									<input value="重置" type="reset" class="button white">
						        </div>
							</div>
						    
						</div>
					</div>
				
				</section>
				<script>
				
					var pages;
					var curr = 1;
					
					var departmentstandard = [];
					var usergroupstandard = [];
				
					window.onload = function(){
						getinfodata(curr);
						
						var oTab = document.getElementById("tabs");
						var oUl = oTab.getElementsByTagName("ul")[0];
						var oLis = oUl.getElementsByTagName("li");
						var oDivs= oTab.getElementsByTagName("div");
						
						for(var i= 0,len = oLis.length;i<len;i++){  
						    oLis[i].index = i;
						    oLis[i].onclick = function() {
						        for(var n= 0;n<len;n++){
						            oLis[n].className = "";
						            oDivs[n].className = "hide";//隐藏的类名
						        }
						     this.className = "on";//显示的类名
						     oDivs[this.index].className = "";
						    };
						}
					}
				
					function getinfodata(page){
						$.ajax({
							url: "userInfoManage!showUserInfoList.action",
							type: "get",
							data: {"page":page},
							datatype: "json",
							contentType: "application/json",
							success: function(jsonResult){
								showInfo(jsonResult);
								
								function resizeInput() {
				   			 	$(this).attr('size', $(this).val().length);
								}
								$("input[type=text]").each(resizeInput);
								$("input[type=email]").each(resizeInput);
								
								var allSelect = document.getElementById("Select");
								var deleteItem =document.getElementById("deleteItem");
								var addItem = document.getElementById("addItem");
								var modifyItem = document.getElementById("modifyItem");
								
								//全选
								allSelect.onclick=function(){
									var inputs=document.getElementsByTagName("input");
									for (var i = 0; i < inputs.length; i++) {
										if(inputs[i].type=="checkbox"){
											if(this.value=="全选"){
												inputs[i].checked="checked";
												
											}else{
												inputs[i].checked="";
												
											}	
										}
									}
									this.value=="全选" ? this.value ="全不选":this.value="全选" ;
								};
					
							
								//删除信息
								deleteItem.onclick=function(){
									var inputs=document.getElementsByTagName("input");
									var flag=0;
									for (var i = 0; i < inputs.length; i++) {
										if(inputs[i].type=="checkbox"){
											if(inputs[i].checked==true){
												flag=1;
											}
										}
									}
									if(flag){
										if(confirm("您确定要删除这些信息吗？")){
										
											var listjson = tojsoninfo();
											
											$.ajax({
												url: "userInfoManage!updateOrDeleteUserInfo.action",
												type: "post",
												contentType: "application/x-www-form-urlencoded;charset=UTF-8",
												data: {"list":listjson,"operate":1},
												datatype: "json",
												success: function(jsonResult){
													alert("删除成功！");
													getinfodata(1);
												},
												error: function(){
													console.log("error");
												}
											});
										}else{
											return false;
										}
									}else{
										alert("您还没有选择要删除的信息！");
										return false;
									}
								};
					
					
								//修改信息
								modifyItem.onclick=function(){
									var inputs=document.getElementsByTagName("input");
									var flag=0;
									for (var i = 0; i < inputs.length; i++) {
										if(inputs[i].type=="checkbox"){
											if(inputs[i].checked==true){
												flag=1;
											}
										}
									}
									if(flag){
										if(confirm("您确定要修改这些信息吗？")){
										
											var listjson = tojsoninfo();
											
											$.ajax({
												url: "userInfoManage!updateOrDeleteUserInfo.action",
												type: "post",
												contentType: "application/x-www-form-urlencoded;charset=UTF-8",
												data: {"list":listjson,"operate":0},
												datatype: "json",
												success: function(jsonResult){
													alert("修改成功！");
													getinfodata(1);
												},
												error: function(){
													console.log("error");
												}
											});
										}else{
											return false;
										}
									
									}else{
										alert("您还没有修改信息！");
										return false;
									}
								};
							    
							    function modify(obj){
							    	var dataID;
						    		if(obj.dataID){
						    			dataID=obj.dataID;
						    		}else if(obj.getAttribute){
						    			dataID=obj.getAttribute("dataID");
						    		}else{
						    			dataID=null;
						    		}
							    
							    	var checkbox =document.getElementById(dataID);
							    	checkbox.checked="checked";
							    	
							    }
							},
							error: function(){
								console.log("fail");
							}
						});
					}
					
					function showInfo(jsonResult){
						var data = eval("("+jsonResult+")");
						var result = "";
						
						if(data == null){
							console.log("fail");
							return ;
						}
						curr = data.pagenum;
						pages = data.pages;
						var list = data.userlist;
						console.log(list);
						departmentstandard = data.departmentlist;
						usergroupstandard = data.userGrouplist;
						
						result += "<table cellspacing='0' align='center' id='myTable'>";
						result += "<tr>";
						result += "<th><input type='button' id='Select' class='button white' value='全选' /></th>";
						result += "<th>员工号</th>";
						result += "<th>员工名</th>";
						result += "<th>性别</th>";
						result += "<th>部门</th>";
						result += "<th>职位</th>";
						result += "<th>用户组</th>";
						result += "<th>邮箱</th>";
						result += "<th>权限</th>";
						result += "</tr>";
						for(var i=0;i<list.length;i++){
							result += "<tr>";
							result += "<td class='avatar'><input type='checkbox' name='check' value='"+list[i].userid+"' id='"+(i+1)+"' />";
							result += "<img src='images/admin.jpg' alt='' height='40' width='40' /></td>";
							result += "<td>"+list[i].userid+"</td>";
							result += "<td><input type='text' name='"+list[i].userName+"' onchange='modify(this)' dataID="+(i+1)+" value='"+list[i].userName+"' /></td>";
							result += "<td><select name='gender' id='gender' style='width:60px'>";
							if(list[i].gender == '男'){
								result += "<option value='男' selected='selected'>男</option>";
								result += "<option value='女'>女</option>";
							}
							else{
								result += "<option value='男'>男</option>";
								result += "<option value='女' selected='selected'>女</option>";
							}
							result += "</select></td>";
							result += "<td><select name='department' id='department'>";
							for(var j=0;j<data.departmentlist.length;j++){
								if(list[i].department.departmentId == data.departmentlist[j].departmentId)
									result += "<option value='"+data.departmentlist[j].departmentId+"' selected='selected'>"+data.departmentlist[j].departmentName+"</option>";
								else
									result += "<option value='"+data.departmentlist[j].departmentId+"'>"+data.departmentlist[j].departmentName+"</option>";	
							}
							result += "</select></td>";
							result += "<td><input type='text' name='"+list[i].position+"' onchange='modify(this)' dataID="+(i+1)+" value='"+list[i].position+"' /></td>";
							result += "<td><select name='userGroup' id='userGroup'>";
							for(var j=0;j<data.userGrouplist.length;j++){
								if(list[i].userGroup.userGroupId == data.userGrouplist[j].userGroupId)
									result += "<option value='"+data.userGrouplist[j].userGroupId+"' selected='selected'>"+data.userGrouplist[j].userGroupName+"</option>";
								else
									result += "<option value='"+data.userGrouplist[j].userGroupId+"'>"+data.userGrouplist[j].userGroupName+"</option>";
							}
							result += "</select></td>";
							result += "<td><input type='text' name='"+list[i].email+"' onchange='modify(this)' dataID="+(i+1)+" value='"+list[i].email+"' /></td>";
							result += "<td><select name='authority' id='authority' >";
							if(list[i].authority == '管理员'){
								result += "<option value='管理员' selected='selected'>管理员</option>";
								result += "<option value='员工'>员工</option>";
							}
							else{
								result += "<option value='管理员'>管理员</option>";
								result += "<option value='员工' selected='selected'>员工</option>";
							}
							result += "</select></td>";
							result += "</tr>";
						}
						result += "</table>";
						$("#useritem").html(result);
						
						laypage({
							cont: 'userpagelist',
							pages: pages,
							curr: curr||1,
							jump: function(obj,first){
								if(!first){
									getinfodata(obj.curr);
								}
							}
						});
					}
					
					//添加信息
					addItem.onclick=function(){
						layer.open({
					        type: 1,
					        skin:'layui-layer-lan',
					        area: ['630px', '450px'],
					        shadeClose: true, //点击遮罩关闭
					        content:$('#popup')
					    });
						
						var departmentselect = document.getElementById("departmentselect");
						var usergroupselect = document.getElementById("usergroupselect");
						
						//删除options
						departmentselect.options.length=0;
						usergroupselect.options.length=0;
						
						for(var i=0;i<departmentstandard.length;i++)
							departmentselect.add(new Option(departmentstandard[i].departmentName,departmentstandard[i].departmentId));
						for(var i=0;i<usergroupstandard.length;i++)
							usergroupselect.add(new Option(usergroupstandard[i].userGroupName,usergroupstandard[i].userGroupId));    
					}
				
					function addinfo(){
						//console.log(departmentselect);
						var data;
						var userid = $("input[name=userid]").val();
						var userName = $("input[name=userName]").val();
						var pwd = $("input[name=pwd]").val();
						var gender = $("#gender option:selected").val();
						var departmentId = $("#departmentselect option:selected").val();
						console.log(departmentId);
						var position = $("input[name=position]").val();
						var userGroupId = $("#usergroupselect option:selected").val();
						console.log(userGroupId);
						var mail = $("input[name=mail]").val();
						console.log(mail);
						var authority = $("#authority option:selected").val();
						
						if(userid == "" || userName == "" || pwd == ""){
							alert("请补充完相关信息！");
							return false;
						}
						
						data = {"userInfo.userid":userid, "userInfo.userName":userName, "userInfo.pwd":pwd,
								"userInfo.gender":gender, "userInfo.department.departmentId": departmentId,
								"userInfo.position":position, "userInfo.userGroup.userGroupId":userGroupId,
								"userInfo.mail":mail, "userInfo.authority":authority
								};
						
						$.ajax({
							url: "userInfoManage!addUserInfo.action",
							type: "post",
							data: data,
							dataType: "json",
							contentType: "application/x-www-form-urlencoded;charset=UTF-8",
							success: function(message){
								if(message!="")	alert(message);
								else{
									alert("增添成功");
									$("input[name=userid]").val("");
									$("input[name=userName]").val("");
									$("input[name=pwd]").val("");
									$("input[name=position]").val("");
									$("input[name=mail]").val("");
								}
								getinfodata(1);
							},
							error: function(){
								alert("error");
							}
						});
					}
					
					
					function tojsoninfo(){
						//集成userInfoList
						var userInfoList = [];	
						
						//得出勾选的项
						var form = document.getElementsByName("check");
						
						for(var i=0;i<form.length;i++){
							if(form[i].checked){
								//获得该tr下中的td数据
								var tr = form[i].parentNode.parentNode;
								//console.log(tr.cells[1].firstChild.value);
								//console.log(tr);
								var userid = form[i].value;
								var userName = tr.cells[2].firstChild.value;
								var gender = tr.cells[3].firstChild.value;
								var departmentId = tr.cells[4].firstChild.value;
								var position = tr.cells[5].firstChild.value;
								var userGroupId = tr.cells[6].firstChild.value;
								var mail = tr.cells[7].firstChild.value;
								var authority = tr.cells[8].firstChild.value;
								var userinfodata = {"userid":userid, "userName":userName,
													"gender":gender, "departmentId": departmentId,
													"position":position, "userGroupId":userGroupId,
													"mail":mail, "authority":authority
													};
								userInfoList.push(userinfodata);
							}
						}
						var listjson = JSON.stringify(userInfoList);
						return listjson;
								
					}
				</script>
				<%@ include file="footer.jsp" %>